import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 添加token到请求头
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API请求错误:', error)
    
    // 处理认证错误
    if (error.response?.status === 401) {
      // token过期或无效，清除本地存储并跳转到登录页
      localStorage.removeItem('token')
      localStorage.removeItem('user')
      window.location.href = '/login'
    }
    
    return Promise.reject(error)
  }
)

// 监控数据API - 匹配后端API结构
export const monitorApi = {
  // 获取监控数据
  getMonitorData: (params?: any) => api.get('/monitor/data', { params }),
  
  // 获取监控统计
  getMonitorStats: (params?: any) => api.get('/monitor/stats', { params }),
  
  // 添加监控数据（监控客户端使用）
  addMonitorData: (data: {
    server_id?: number;
    ip_address?: string;
    metrics: {
      cpu: number;
      memory: number;
      disk: number;
    };
  }) => api.post('/monitor/data', data),
}

// 用户认证API
export const authApi = {
  login: (data: { username: string; password: string }) => api.post('/auth/login', data)
}

// 用户管理API
export const userApi = {
  // 获取用户列表
  getUsers: () => api.get('/users'),
  
  // 获取指定用户信息
  getUser: (userId: number) => api.get(`/users/${userId}`),
  
  // 创建用户
  createUser: (userData: {
    username: string;
    password: string;
    email: string;
    role: string;
  }) => api.post('/users', userData),
  
  // 添加用户（别名）
  addUser: (userData: {
    username: string;
    password: string;
    email: string;
    role: string;
  }) => api.post('/users', userData),
  
  // 更新用户
  updateUser: (userId: number, userData: {
    username?: string;
    email?: string;
    role?: string;
    password?: string;
  }) => api.put(`/users/${userId}`, userData),
  
  // 删除用户
  deleteUser: (userId: number) => api.delete(`/users/${userId}`),
  
  // 获取当前用户信息
  getProfile: () => api.get('/profile'),
  
  // 重置用户密码
  resetPassword: (username: string, password: string) => 
    api.put('/users/reset-password', { username, password })
}

// 服务器管理API
export const serverApi = {
  // 获取服务器列表
  getServers: () => api.get('/servers'),
  
  // 获取指定服务器信息
  getServer: (serverId: number) => api.get(`/servers/${serverId}`),
  
  // 添加服务器
  addServer: (serverData: {
    server_name: string;
    ip_address: string;
    port: number;
    user_ids: number[];
  }) => api.post('/servers', serverData),
  
  // 更新服务器
  updateServer: (serverId: number, serverData: {
    server_name?: string;
    ip_address?: string;
    port?: number;
    status?: string;
    user_ids?: number[];
  }) => api.put(`/servers/${serverId}`, serverData),
  
  // 删除服务器
  deleteServer: (serverId: number) => api.delete(`/servers/${serverId}`),
  
  // 获取用户关联的服务器列表
  getUserServers: (userId?: number) => api.get('/user-servers', { params: { user_id: userId } }),
  
  // 为服务器添加用户
  addServerUser: (serverId: number, userId: number) => 
    api.post(`/servers/${serverId}/users`, { user_id: userId }),
  
  // 从服务器移除用户
  removeServerUser: (serverId: number, userId: number) => 
    api.delete(`/servers/${serverId}/users/${userId}`)
}

export default api
